<div class="tab-pane" id="analytics">
  <div class="clearfix">
    <div class="pull-right">
      <%= render 'admin/action_pages/date_range_form' -%>
    </div>
  </div>
  <br>
  <hr />
  <div class="row">
    <div class="col-sm-12 col-lg-12 col-md-12">
      <h3>Action Page Views</h3>
      <%= if @date_start == @date_end
        @data = ::Ahoy::Event.views.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
      else
        @data = ::Ahoy::Event.views.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
      end
      line_chart(
        [{
          name: "Total Page Views",
          data: @data
        }],
        library: { responsive: true, canvasDimensions: {width: 500, height: 200}}
      ) %>

      <h3>Email Tool</h3>
      <%= if @date_start == @date_end
        @data = ::Ahoy::Event.emails.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
      else
        @data = ::Ahoy::Event.emails.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
      end
      line_chart(
        [{
          name: "Total Emails",
          data: @data
        }],
        library: {responsive: true, canvasDimensions: {width: 500, height: 200}}
      ) %>

      <h3>Congress Messages Sent</h3>
      <%= if @date_start == @date_end
        line_chart(
          congress_forms_hour_fills_url(nil, @date_start),
          library: {responsive: true, canvasDimensions: {width: 500, height: 200}}
        )
      else
        line_chart(
          congress_forms_date_fills_url(nil, @date_start, @date_end),
          library: {responsive: true, canvasDimensions: {width: 500, height: 200}}
        )
      end %>

      <h3>Call Tool</h3>
      <%= if @date_start == @date_end
        @data = ::Ahoy::Event.calls.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
      else
        @data = ::Ahoy::Event.calls.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
      end
      line_chart(
        [{
          name: "Total Calls",
          data: @data
        }],
        library: {responsive: true, canvasDimensions: {width: 500, height: 200}}
      ) %>

      <h3>Tweet Tool</h3>
      <%= if @date_start == @date_end
        @data = ::Ahoy::Event.tweets.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
      else
        @data = ::Ahoy::Event.tweets.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
      end
      line_chart(
        [{
          name: "Total Tweets",
          data: @data
        }],
        library: {responsive: true, canvasDimensions: {width: 500, height: 200}}
      ) %>

      <h3>Petition Tool</h3>
      <%= if @date_start == @date_end
        @data = ::Ahoy::Event.signatures.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
      else
        @data = ::Ahoy::Event.signatures.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
      end
      line_chart(
        [{
          name: "Total Signatures",
          data: @data
        }],
        library: { responsive: true, canvasDimensions: {width: 500, height: 200}}
      ) %>

      <h3>Users</h3>
      <%= if @date_start == @date_end
        @data = User.where('created_at BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:created_at, format: '%Y-%m-%d %H:%M:00 UTC').count
      else
        @data = User.where('created_at BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:created_at, format: '%Y-%m-%d 00:00:00 UTC').count
      end
      line_chart(
        [{
          name: "Registrations",
          data: @data
        }],
        library: { responsive: true, canvasDimensions: {width: 500, height: 200}}
      ) %>
      <div class="clearfix"> </div>
    </div>
  </div>
</div>
